#include "fig_exploding.h"
#include "fighter.h"

#include "math/facing.h"
#include "part2d/part2d.h"
#include "num/rand.h"
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros

const float  FIGExploding::DISAPPEAR_DURATION = 0.1f;


FIGExploding::FIGExploding(FIGExploding::EXPLODING_TYPES Type) : 
								m_Type(Type),
								m_RotationSpeed(0),
								m_DisappearCounter(0),
								m_bShouldRandomRotate (true),
								m_Alpha(1.0f),
								m_Scale(1.0f)
{
	
	MapTypeToName();
	
	
}

FIGExploding::~FIGExploding()
{
	for(int i = 0; i < (int) m_pParticles.size(); i++)
	{
		part2d::Part2D* pPar = m_pParticles[i];
		UTIL_SAFE_DELETE(pPar);
	}
	m_pParticles.resize(0);
	m_TriggerCounts.resize(0);
}


void	FIGExploding::Render(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;

	Scale = Scale * m_Scale;
	if((int)m_pParticles.size() > 0)
	{
		for(int i = 0; i < (int)m_pParticles.size(); i++)
		{
			part2d::Part2D* pParticle =  m_pParticles[i];
			//pParticle->SetPos();
			pParticle->Render(-Offset/Scale , Scale);
		}
	}
	else
	{
		m_Sprite.m_Pos = m_Pos + Offset + m_RandomMovePos;
		m_Sprite.m_Size = m_Size * Scale;
		m_Sprite.m_Rotation = m_Rotation;
		m_Sprite.m_Color = ColorfRGBA(1.0f,1.0f,1.0f,m_Alpha);
		m_Sprite.RenderRotated();

	}
}

void	FIGExploding::RenderDebug(const math::Vec2& Offset,float Scale)
{

}

void	FIGExploding::Tick(float dt)
{
	if(!m_IsAlive)
		return;

	if(m_bShouldRandomRotate)
		m_Rotation += (m_RotationSpeed * dt);
	m_RandomMovePos += (m_RandomDir * dt);
	m_Alpha -= dt;
	
	bool Finish = false;
	if((int)m_pParticles.size() > 0)
	{
		for(int i = 0; i < (int)m_pParticles.size(); i++)
		{
			part2d::Part2D* pParticle =  m_pParticles[i];
			bool Temp = Finish;
			m_IsAlive = pParticle->Tick(dt);
			if(Temp)
				m_IsAlive = true;
		}
	}
	else
	{

		if( m_DisappearCounter > 0)
		{
			m_DisappearCounter-= dt;
			if( m_DisappearCounter <= 0)
				m_IsAlive = false;
			return;
		}

		else if(!m_Sprite.Tick(dt))
		{
			m_IsAlive = false;
		}
	}

	
}


void	FIGExploding::Clear()
{

}


void			FIGExploding::Load(const std::string& Name)
{
  	m_Sprite.Create(Name);
	m_Size = m_Sprite.m_Size;
}

void			FIGExploding::ParticleLoad(const std::string& Name,int	TriggerCount)
{

	part2d::Part2D* pParticle = new part2d::Part2D();
	pParticle->Load(Name);
	m_pParticles.push_back(pParticle);
	m_TriggerCounts.push_back(TriggerCount);

}

void			FIGExploding::ReSpawn(const math::Vec2& Pos, 
									  float ScaleSize,
									  float Rotation									  
									  )
{
	if(ScaleSize < 1 || ScaleSize > 3)
		bool test = 0;
	m_Scale = ScaleSize;
	m_Pos = Pos;
	m_Rotation = Rotation;
	float Scale = Fighter::Instance().GetScale();
	Scale = Scale * m_Scale;
	m_RandomMovePos = math::Vec2(0,0);

	if(m_Type > CRAFT_PIECES)		
		Randomize();

	
	if((int)m_pParticles.size() > 0)
	{
		for(int i = 0; i < (int)m_pParticles.size(); i++)
		{
			part2d::Part2D* pParticle =  m_pParticles[i];
			math::Vec2 Dir = math::Vec2(0,0);
			if(m_bShouldRandomRotate)
			{
				Dir = math::Facing::VectorFromAngle((float)num::RAND32::Instance().RandInt(0,364));
				Dir *= 0.1f;
			}
			else
			{
				math::Facing::VectorFromAngle(-45);
				Dir *= 0.1f;
			}
			
			int TriggerCount = m_TriggerCounts[i];
			if(m_bShouldRandomRotate)
			{
				
				for(int j = 0; j < TriggerCount; j++)
				{
					Dir = math::Facing::VectorFromAngle((float)num::RAND32::Instance().RandInt(0,364));
					Dir *= 0.03f;
					pParticle->TriggerParticleSet(1, m_Pos / Scale   ,Dir);
				}
			}
			else
				pParticle->TriggerParticleSet(TriggerCount, m_Pos / Scale );
		}
	}


	m_IsAlive = true;
	m_DisappearCounter = m_DisappearDuration;
}

void		FIGExploding::MapTypeToName()
{
	switch(m_Type)
	{
		case  FIGExploding::EXPLODING_TYPE_NORMAL:
			
			ParticleLoad("effects/pickupweapon.p2d",1);
			ParticleLoad("effects/fire.p2d",2);			
			
			m_bShouldRandomRotate = true;			
			//m_DisappearDuration = 0.5f;
			//m_DisappearCounter = m_DisappearDuration;
			
		break;
		case  FIGExploding::EXPLODING_TYPE_SmallHIT:
  			Load("hit");
			m_DisappearDuration = DISAPPEAR_DURATION;
			m_DisappearCounter = m_DisappearDuration;
		break;
		case FIGExploding::EXPLODING_ROKETSMOKE:
			ParticleLoad("effects/ice_hit.p2d",1);
		break;
		case TRAIL_JET:
			//ParticleLoad("effects/fighter_jet_trail.p2d",1);
			//m_bShouldRandomRotate = false;
		break;
		case CRAFT_PIECES_TRIANGLE_FRAME:
			Load("Triangle_frame");			
			Randomize();
			m_bShouldRandomRotate = false;
		break;
		case CRAFT_PIECES_TRIANGLE_MAIN:
			Load("Triangle_1");
			m_bShouldRandomRotate = true;
			Randomize();
		break;
		case PICKUP_TRAIL:
			ParticleLoad("effects/tokenstart.p2d",1);
			m_bShouldRandomRotate = false;
		break;
		case PICKUP_PICKED:
			ParticleLoad("effects/fighter_pickup_picked.p2d",1);
			m_bShouldRandomRotate = false;
		break;
	}
}


void		FIGExploding::Randomize()
{
	
	m_Alpha = 1.0f;

	m_RotationSpeed =  num::RAND32::Instance().RandFloat( 180.0f,359.0f);
	if(num::RAND32::Instance().RandInt(0,1) == 0)
		m_RotationSpeed  *= -1;

	m_RandomDir = math::Vec2(num::RAND32::Instance().RandFloat( -1.0f,1.0f),
							 num::RAND32::Instance().RandFloat( -1.0f,1.0f));

	m_RandomDir *= num::RAND32::Instance().RandFloat( 0.1f,0.15f);
	m_DisappearCounter = m_DisappearDuration = num::RAND32::Instance().RandFloat( 0.25f,0.35f);
	
}